package models

import (
	"time"

	"gorm.io/gorm"
)

type OperationLog struct {
	ID         uint      `json:"id" gorm:"primaryKey"`
	UserID     uint      `json:"user_id" gorm:"not null"`
	Action     string    `json:"action" gorm:"size:50;not null"`
	Resource   string    `json:"resource" gorm:"size:50;not null"`
	ResourceID *uint     `json:"resource_id"`
	Details    string    `json:"details" gorm:"type:text"`
	IPAddress  string    `json:"ip_address" gorm:"size:45"`
	UserAgent  string    `json:"user_agent" gorm:"type:text"`
	CreatedAt  time.Time `json:"created_at"`

	// 关联
	User User `json:"user" gorm:"foreignKey:UserID"`
}

// TableName 指定表名
func (OperationLog) TableName() string {
	return "operation_logs"
}

// GetActionText 获取操作文本
func (ol *OperationLog) GetActionText() string {
	actionMap := map[string]string{
		"create": "创建",
		"update": "更新",
		"delete": "删除",
		"view":   "查看",
		"login":  "登录",
		"logout": "登出",
		"push":   "推送",
		"export": "导出",
		"import": "导入",
	}
	if text, ok := actionMap[ol.Action]; ok {
		return text
	}
	return ol.Action
}

// GetResourceText 获取资源文本
func (ol *OperationLog) GetResourceText() string {
	resourceMap := map[string]string{
		"user":        "用户",
		"role":        "角色",
		"permission":  "权限",
		"customer":    "客户",
		"partner":     "合作机构",
		"push_record": "推送记录",
		"system":      "系统",
	}
	if text, ok := resourceMap[ol.Resource]; ok {
		return text
	}
	return ol.Resource
}

// CreateLog 创建操作日志
func CreateLog(db *gorm.DB, userID uint, action, resource string, resourceID *uint, details, ipAddress, userAgent string) error {
	log := OperationLog{
		UserID:     userID,
		Action:     action,
		Resource:   resource,
		ResourceID: resourceID,
		Details:    details,
		IPAddress:  ipAddress,
		UserAgent:  userAgent,
	}
	return db.Create(&log).Error
}

// LogUserLogin 记录用户登录
func LogUserLogin(db *gorm.DB, userID uint, ipAddress, userAgent string) error {
	return CreateLog(db, userID, "login", "system", nil, "用户登录", ipAddress, userAgent)
}

// LogUserLogout 记录用户登出
func LogUserLogout(db *gorm.DB, userID uint, ipAddress, userAgent string) error {
	return CreateLog(db, userID, "logout", "system", nil, "用户登出", ipAddress, userAgent)
}

// LogCustomerCreate 记录客户创建
func LogCustomerCreate(db *gorm.DB, userID uint, customerID uint, details, ipAddress, userAgent string) error {
	return CreateLog(db, userID, "create", "customer", &customerID, details, ipAddress, userAgent)
}

// LogCustomerUpdate 记录客户更新
func LogCustomerUpdate(db *gorm.DB, userID uint, customerID uint, details, ipAddress, userAgent string) error {
	return CreateLog(db, userID, "update", "customer", &customerID, details, ipAddress, userAgent)
}

// LogCustomerDelete 记录客户删除
func LogCustomerDelete(db *gorm.DB, userID uint, customerID uint, details, ipAddress, userAgent string) error {
	return CreateLog(db, userID, "delete", "customer", &customerID, details, ipAddress, userAgent)
}

// LogCustomerPush 记录客户推送
func LogCustomerPush(db *gorm.DB, userID uint, customerID uint, details, ipAddress, userAgent string) error {
	return CreateLog(db, userID, "push", "customer", &customerID, details, ipAddress, userAgent)
}